import AITalk from '../talkTemplate';
import { GetChangedNextStepMethod } from './defination';
const node = AITalk.getTalks('variableJudge');
/**
 * 获取下一步变更的数据
 * @param { Object } preNext 初始的下一步数据 {other: ''}
 * @param { Object } curNext 当前的下一步数据  {other: ''}
 * @param { Array } preVariableBranchList 初始的变量列表  []
 * @param { Array } curVariableBranchList 当前的变量列表  []
 */
export const getChangedNextStep = (preNext = {}, curNext = {}, preVariableBranchList = [], curVariableBranchList = []) => {
	let changedNextSteps = [];
	if (!preNext && !curNext && !preVariableBranchList && !curVariableBranchList) {
		return [];
	}
	if (preVariableBranchList.length > curVariableBranchList.length) {
		// 有删除
		preVariableBranchList.map((item, index) => {
			if (curVariableBranchList[index]) {
				if (item.nextStepsAnswer !== curVariableBranchList[index].nextStepsAnswer) {
					changedNextSteps.push({
						text: `分支${index + 1}`,
						type: `branch${index + 1}`,
						previous: item.nextStepsAnswer,
						current: curVariableBranchList[index].nextStepsAnswer
					});
				}
			} else {
				changedNextSteps.splice(0, 0, {
					text: `分支${index + 1}`,
					type: `branch${index + 1}`,
					previous: item.nextStepsAnswer,
					current: null
				});
			}
		});
	} else {
		// 有新增
		curVariableBranchList.map((item, index) => {
			console.log(item, index);
			if (preVariableBranchList[index]) {
				if (item.nextStepsAnswer !== preVariableBranchList[index].nextStepsAnswer) {
					changedNextSteps.push({
						text: `分支${index + 1}`,
						type: `branch${index + 1}`,
						previous: preVariableBranchList[index].nextStepsAnswer,
						current: item.nextStepsAnswer
					});
				}
			} else {
				changedNextSteps.push({
					text: `分支${index + 1}`,
					type: `branch${index + 1}`,
					previous: null,
					current: item.nextStepsAnswer
				});
			}
		});
	}

	if (preNext['other'] !== curNext['other']) {
		changedNextSteps.push({
			text: '其他分支',
			type: node.answer[0].type,
			previous: preNext['other'],
			current: curNext['other']
		});
	}

	return changedNextSteps;
};
GetChangedNextStepMethod['variableJudge'] = getChangedNextStep;
